package com.woniuxy.services.impl;

import com.woniuxy.daos.PermissionDao;
import com.woniuxy.entitys.Permission;
import com.woniuxy.entitys.TreeNode;
import com.woniuxy.services.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //根据用户角色获取权限
    @Override
    public List<Permission> getMenuList(Integer role_id, Integer pid) {
        return permissionDao.getPers(role_id,pid);
    }

    //获取所有权限
    @Override
    public List<TreeNode> getAllPers(Integer per_pid) {
        //创建待返回的树节点集合
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();

        //pid = 0 获取所有一级权限
        List<Permission> rootPers = permissionDao.getAllPers(per_pid);
        //若存在一级权限，则遍历所有一级权限
        if(rootPers.size() > 0){
            for(Permission per: rootPers){
                //将每个一级权限封装成treenote形式
                TreeNode treeNode = getTreeNode(per);
                //添加treenote到待返回的集合中
                treeNodes.add(treeNode);
            }
        }
        return treeNodes;
    }

    //根据role_id获取已有权限id
    @Override
    public List<Integer> getPersIdByRid(Integer role_id) {
        return permissionDao.selectPerIdsByRid(role_id);
    }

    // 保存分配权限
    @Override
    public Integer savePers(Integer role_id, ArrayList<Integer> perIds) {
        Integer del = permissionDao.deletePersByRid(role_id);
        Integer add = permissionDao.insertPersByRid(role_id,perIds);
        return  del+add ;
    }
    //获取所有权限及children
    @Override
    public List<Permission> getPersList() {
        //查询pid=0的一级权限
        List<Permission> rootList = permissionDao.getPersList(0);
        if(rootList.size() > 0 ){
            //获取该父权限的子权限
            for(Permission per : rootList){
                getChildrenPersList(per);
            }
        }
        System.out.println(rootList);
        return rootList;

    }

    private void getChildrenPersList(Permission rootper){
        List<Permission> list = permissionDao.getPersList(rootper.getPer_id());
        rootper.setChildren(list);
        if(list.size() > 0){
            for(Permission per : list){
                getChildrenPersList(per);
            }
        }
    }

    //获取所有菜单权限
    @Override
    public List<Permission> getMenuListWithoutRid() {

        return permissionDao.getMenuListWithoutRid() ;
    }

    //保存新增权限
    @Override
    public Integer addPer(Permission permission) {
        return permissionDao.insertSelective(permission);
    }

    //修改权限状态
    @Override
    public Integer updPerById(Integer per_id, String per_remark) {
        return permissionDao.updPerById(per_id,per_remark);
    }

    //根据查询语句查询相应权限
    @Override
    public List<Permission> searchPers(String value) {
        return permissionDao.searchPers(value);
    }

    //将一个权限封装成treenote格式
    public  TreeNode getTreeNode(Permission permission){
        //创建一个treenote对象
        TreeNode treeNode = new TreeNode();
        //设置该对象的id为权限id
        treeNode.setId(permission.getPer_id());
        //设置该对象的文本内容为权限的名称
        treeNode.setLabel(permission.getPer_name());
        //通过传入参数permission中的id查找是否有对应的子权限
        List<Permission> limbPers = permissionDao.getAllPers(permission.getPer_id());
        //若存在子权限
        if(limbPers.size() > 0){
            //创建一个treenote子集合作为传入的父权限的children属性待返回
            List<TreeNode> children = new ArrayList<TreeNode>();
            //遍历每一个子权限
            for(Permission per:limbPers){
                //将每一个子权限封装为一个treenote对象的格式
                TreeNode treeNodeChild = getTreeNode(per);
                //将子权限封装的treenote加入treenote子集合
                children.add(treeNodeChild);
            }
            //将treenote子集合设置为父权限的children属性
            treeNode.setChildren(children);
        }
        return  treeNode;
    }
}
